package com.example.system.web;

import com.example.common.core.Result;
import com.example.system.annotation.HasPermission;
import com.example.system.dto.AdminParam;
import com.example.system.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Api(value = "管理员API", tags = "管理员API")
@RestController
@RequestMapping("/system/api/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @HasPermission("system:admin:add")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    public Result add(@RequestBody AdminParam adminParam) {
        return adminService.add(adminParam);
    }

    @HasPermission("system:admin:delete")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return adminService.delete(id);
    }

    @HasPermission("system:admin:delete")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam(value = "ids") List<Integer> ids) {
        return adminService.deleteBatch(ids);
    }

    @HasPermission("system:admin:update")
    @ApiOperation(value = "更新", notes = "更新")
    @PutMapping("/update")
    public Result update(@RequestBody AdminParam adminParam) {
        return adminService.update(adminParam);
    }

    @HasPermission("system:admin:select")
    @ApiOperation(value = "查询", notes = "查询")
    @GetMapping("/{id}")
    public Result select(@PathVariable("id") Integer id) {
        return adminService.select(id);
    }

    @HasPermission("system:admin:selectList")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    @GetMapping("/selectList")
    public Result selectList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        return adminService.selectList(page, size);
    }

    @ApiOperation(value = "上传头像",notes = "上传头像")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam(value = "avatar") MultipartFile avatar, @RequestParam(value = "id") Integer id) throws IOException {
        return adminService.uploadAvatar(avatar,id);
    }
}
